﻿using Abp.Application;
using Abp.Application.Services;
using Abp.Application.Transactions;
using Abp.Configurations;
using Abp.Dependency;
using Abp.Domain.Entities;
using Abp.NHibernate;
using Castle.Core.Logging;
using FluentNHibernate.Cfg.Db;
using System;
using System.Collections.Generic;
using System.Configuration;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Abp.Configurations.Configuration.Create()
               .UseCastle()
               .RegisterCommonComponents()
               .UseLog4Net()
               .RegisterUnhandledExceptionHandler()
               .UseTransactionScope()
               .SetDefault<IAccountManager,AccountManager>();
        
            var loger=ObjectContainer.Resolve<ILogger>();
            loger.Debug("aaaaaaaa");

         
            ObjectContainer.Resolve<IAccountManager>().GetAccount();
            IPersistenceConfigurer congigurer = MsSqlConfiguration.MsSql2008.ConnectionString(ConfigurationManager.ConnectionStrings["Default"].ConnectionString);
            NhibernateHelper.Instance(congigurer,
                NhibernateHelper.THREAD_STATIC,
                typeof(Program).Assembly
                );

            AccountRepository repository = new AccountRepository();
            //List<Account> accounts = new List<Account>();

            //for (int i = 0; i < 100; i++)
            //{
            //    accounts.Add(new Account() {
            //        AccountId=Abp.Utils.RandomUtils.RandomString(32),
            //        UserName=Abp.Utils.RandomUtils.RandomString(12)
            //    });
            //}
            //repository.Insert(accounts);

            //repository.GetByPage(new Abp.Web.Pagination() { Page = 2, Rows = 10, Sort = "UserName", Order = "asc" },
            //    info=>info.AccountId.StartsWith("a")
            //    ,null);

            repository.GetByPage(
                new Abp.Web.Pagination() { Page = 2, Rows = 10},
                info => info.AccountId.StartsWith("张"), 
                (entity)=> {
                    entity.Desc(info => info.AccountId, info => info.UserName);
                });
            Console.Read();
        }

     

      
    }


    public interface IAccountManager: IServiceManager
    {
        void GetAccount();
    }

    public class AccountManager : ServiceManagerImpl, IAccountManager
    {
        [Transaction]
        public void GetAccount()
        {
            Console.WriteLine("GetAccount");
           
        }
    }

    public class Account:IEntity
    {
        public virtual string AccountId { get; set; }

        public virtual string UserName { get; set; }
    }

    public class AccountMap : NhEntityMapper<Account>
    {
        public AccountMap()
        {
           
            this.Id(info => info.AccountId).GeneratedBy.Assigned();
            this.Map(info => info.UserName);
        }
    }

    public class AccountRepository : NhRepository<Account>
    {

    }
}
